package com.weilele.leisure.time.ui.module.search


import android.content.res.ColorStateList
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import android.view.Gravity
import androidx.constraintlayout.widget.ConstraintLayout
import com.weilele.base.library.BaseBindingFragment
import com.weilele.base.library.SwitchView
import com.weilele.leisure.time.R
import com.weilele.leisure.time.bean.db.CommonDb
import com.weilele.leisure.time.databinding.FragmentHistorySearchBinding
import com.weilele.mvvm.base.helper.annotation.MvvmActivityViewModel
import com.weilele.mvvm.base.livedata.appCompatActivity
import com.weilele.mvvm.base.livedata.LiveDataWrap
import com.weilele.mvvm.base.livedata.success
import com.weilele.mvvm.base.livedata.wrapSuccessObserver
import com.weilele.mvvm.utils.activity.dip
import com.weilele.mvvm.utils.activity.dp
import com.weilele.mvvm.utils.activity.getResColor
import com.weilele.mvvm.utils.activity.onClick
import com.weilele.mvvm.view.SimpleSwitchView
import com.weilele.mvvm.widget.BaseTextView
import com.weilele.leisure.time.bean.db.HistorySearchBean

class HistorySearchFragment : BaseBindingFragment<FragmentHistorySearchBinding>() {
    @MvvmActivityViewModel
    private lateinit var searchViewModel: SearchViewModel
    override fun getObserverLiveData(): List<LiveDataWrap>? {
        return listOf(
            CommonDb.historySearchData wrapSuccessObserver ::onSearchHistory,
        )
    }

    override fun initUi(savedInstanceState: Bundle?) {
        super.initUi(savedInstanceState)
        mBinding.tvClearHistory.onClick {
            CommonDb.deleteSearchHistory(null)
            onSearchHistory(mutableListOf())
        }
    }

    override fun isNeedSwitchView(): Boolean {
        return true
    }

    private fun onSearchHistory(datas: MutableList<HistorySearchBean>) {
        if (datas.isNullOrEmpty()) {
            showOtherView(SwitchView.OtherBean("试着去搜点什么~",R.drawable.icon_empty))
        } else {
            showContentView()
        }
        mBinding.flowLayout.setViews(datas.mapIndexed { index, historySearchBean ->
            BaseTextView(appCompatActivity!!).apply {
                text = historySearchBean.keyword
                layoutParams = ConstraintLayout.LayoutParams(
                    ConstraintLayout.LayoutParams.WRAP_CONTENT,
                    dip(28)
                )
                textSize = 13f
                gravity = Gravity.CENTER
                background = GradientDrawable().apply {
                    cornerRadius = 14f.dp
                    color = ColorStateList.valueOf(getResColor(R.color.colorPrimary))
                }
                setTextColor(getResColor(R.color.colorAppTextSecondary))
                setPadding(dip(12), 0, dip(12), 0)
                onClick {
                    searchViewModel.clickHistorySearchData.success(historySearchBean)
                }
                setOnLongClickListener {
                    CommonDb.deleteSearchHistory(historySearchBean)
                    return@setOnLongClickListener true
                }
            }
        })
    }

    override fun initData() {
        super.initData()
        CommonDb.findAllSearchHistory()
    }
}